class UtilTemplate {
    /** 变量定义 */
    private varTemp: string = `
\t\t/** {desc} */
\t\t{varName} : {varType};`;
    /** 接口定义 */
    private interfaceTemp: string = `\t/** {desc} */
\tinterface {name} {
{varList} 
\t}\n`;

    private readonly nsTemp = 'declare namespace msg.auto {\n'


    /**
     * 拼接变量字符串
     * @param varList 变量数据列表
     * @returns 
     */
    private genVarStr(varList: any[]): string {
        if (varList && varList.length > 0) {
            let str = '';
            const list: string[] = []
            varList.forEach((item) => {
                let line = this.varTemp.replace('{varName}', item.name);
                line = line.replace('{varType}', item.type);
                line = line.replace('{desc}', item.description);
                list.push(line);
            });
            if (list.length > 0) {
                str = list.join('\n');
                return str;
            }
        }
        return '';
    }

    public genInterface(map: any): string {
        let str = '';
        let tag = '';
        for (const key in map) {
            const item = map[key];
            const varStr = this.genVarStr(item.varList);
            let interfaceStr = this.interfaceTemp.replace('{desc}', item.desc);
            if (item.tags) {
                if (tag != item.tags) {
                    tag = item.tags;
                    interfaceStr = '//MARK: ' + tag + '\n' + interfaceStr;
                }
            }
            let n = item.name;
            if (item.nameTail) {
                n += item.nameTail;
            }
            interfaceStr = interfaceStr.replace('{name}', n);
            interfaceStr = interfaceStr.replace('{varList}', varStr);

            str += interfaceStr;
        }
        str = this.nsTemp + str + '\n}';
        return str;
    }
}

export const utilTemplate = new UtilTemplate();